package com.zut.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zut.yyzx.dto.BackdownDTO;
import com.zut.yyzx.pojo.Backdown;
import com.zut.yyzx.pojo.Bed;
import com.zut.yyzx.pojo.Customer;
import com.zut.yyzx.service.BackdownService;
import com.zut.yyzx.service.BedService;
import com.zut.yyzx.service.CustomerService;
import com.zut.yyzx.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/backdown")
//"退住管理"
@CrossOrigin
public class BackdownController {
    @Autowired
    private BackdownService backdownService;
    @Autowired
    private BedService bedService;
    @Autowired
    private CustomerService customerService;
//    private
    @PostMapping("/listBackdown")
    //"查询退住信息"
    public ResultVo listBackdown(BackdownDTO backdownDTO) throws Exception{
        return backdownService.listBackdownVo(backdownDTO);
    }
    //"添加退住审批"
    @PostMapping("/addBackdown")
    public ResultVo addBackdown(Backdown backdown) throws  Exception{
        backdownService.save(backdown);
        return ResultVo.ok("添加成功");
    }
    @PostMapping("/examineBackdown")
    //"审批退住"
    public ResultVo examineBackdown(Backdown backdown) throws Exception{
        Backdown bd = backdownService.getById(backdown.getId());
        //审批通过
        if(backdown.getAuditstatus() == 1){
            //修改床铺记录，对应床铺状态改为空闲
            Customer cs = customerService.getById(bd.getCustomerId());
            Bed bed = new Bed();
            bed.setId(cs.getBedId());
            bed.setBedStatus(1);
            bedService.updateById(bed);
        }
        return backdownService.examineBackdown(backdown);
    }
    //"撤回退住申请"
    @PostMapping("/delBackdown")
    public ResultVo delBackdown(Integer id) throws  Exception{
        UpdateWrapper<Backdown> updateWrapper = new UpdateWrapper<Backdown>();
        updateWrapper.eq("id",id);
        updateWrapper.set("is_deleted",1);
        backdownService.update(updateWrapper);
        return ResultVo.ok("撤回成功");
    }



}
